<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\home\controller;


class UpgradeController extends BaseController
{

	/**
	*
	*
	*升级-页面
	**/
	public function index()
	{
		$member_id = $this->member_id;
		$memModel = new \app\home\model\MemberModel();

		//查找用户信息
		$meminfo = $memModel->getMemberIdWhereMemberId($member_id, 'member_id, logo, nickname, mobile, parent, grade, invite, status');

		switch ($meminfo['grade']) {
			case 1:
				$orderModel = new \app\home\model\OrdersModel();
				$meminfo['after_1'] = !empty($meminfo['parent']) ? 1 : 0;

				//消费金额计算
				$money = $orderModel->getMemberOrderSuccess($member_id);
				$meminfo['after_2'] = $money;
				break;



			case 2:
				//头部
				$meminfo['line_1'] = [1, 1, 1];
				$meminfo['line_2'] = [1, 1, 1];
				$meminfo['line_3'] = [1, 1, 1];
				$meminfo['line_4'] = [0, 1, 1];
				$meminfo['line_5'] = [0, 1, 1];
				$meminfo['line_6'] = [0, 0, 1];

				//中部
				$number = $memModel->geetMemberInviteGradeTwoCount($meminfo['invite'], 2);
				$meminfo['after_1'] = $number;

				$number2 = $this->teamConsultant($meminfo['invite'], 2);
				$meminfo['after_2'] = $number2;

				$number3 = $this->teamConsultantOrderSuccess($meminfo['invite']);
				$meminfo['after_3'] = $number3;

				$oaModel = new \app\home\model\OrderAwardModel();
				$number4 = $oaModel->memberOrderEarnings($meminfo['member_id']);
				$meminfo['after_4'] = $number4;
				break;


			case 3:
				//头部
				$meminfo['line_1'] = [1, 1, 1];
				$meminfo['line_2'] = [1, 1, 1];
				$meminfo['line_3'] = [1, 1, 1];
				$meminfo['line_4'] = [0, 1, 1];
				$meminfo['line_5'] = [0, 1, 1];
				$meminfo['line_6'] = [0, 0, 1];

				//中部
				$number = $memModel->geetMemberInviteGradeTwoCount($meminfo['invite'], 3);
				$meminfo['after_1'] = $number;

				$number2 = $this->teamConsultant($meminfo['invite'], 2);
				$meminfo['after_2'] = $number2;

				$number3 = $this->teamConsultantOrderSuccess($meminfo['invite']);
				$meminfo['after_3'] = $number3;

				$oaModel = new \app\home\model\OrderAwardModel();
				$number4 = $oaModel->memberOrderEarnings($meminfo['member_id']);
				$meminfo['after_4'] = $number4;
				break;


			case 4:
				//头部
				$meminfo['line_1'] = [1, 1, 1];
				$meminfo['line_2'] = [1, 1, 1];
				$meminfo['line_3'] = [1, 1, 1];
				$meminfo['line_4'] = [0, 1, 1];
				$meminfo['line_5'] = [0, 1, 1];
				$meminfo['line_6'] = [0, 0, 1];
				break;
			
			default:
				$this->error('等级错误，请联系管理员');
				break;
		}

		//隐藏手机号中间四位数
		if(!empty($meminfo['mobile']))
		{
			$meminfo['mobile'] = substr_replace($meminfo['mobile'],'****',3,4);
		}

		$this->assign('info', $meminfo);
		return $this->fetch('upgrade/index');
	}


	/**
	*
	*团队顾问人数
	*$grade =2 顾问， =3星级顾问
	*
	**/
	protected function teamConsultant($invite, $grade = 2)
	{
		$memModel = new \app\home\model\MemberModel();
		$list = $memModel->geetMemberTeamNumber($invite);
		$list = !empty($list) ? $list : [];

		$team_number = 0;
		foreach ($list as $key => $value) {
			if($value['grade'] == $grade)
			{
				$team_number = $team_number + 1;
			}
		}


		foreach($list as $val)
		{
			$number = $this->teamConsultant($val['invite'], $grade);
			$team_number = $team_number + $number;
		}
		return $team_number;
	}


	/**
	*
	*团队-消费5元以上的用户
	*
	**/
	protected function teamConsultantOrderSuccess($invite)
	{
		$memModel = new \app\home\model\MemberModel();
		$orderModel = new \app\home\model\OrdersModel();
		$list = $memModel->getMemberTeamOrderNumber($invite);
		$list = !empty($list) ? $list : [];
		
		$count_number = 0;
		foreach($list as $val)
		{
			$number = $this->teamConsultantOrderSuccess($val['invite']);
			$count_number = $count_number + $number;
			$money = $orderModel->getMemberOrderSuccess($val['member_id']);
			if($money >= 5)
			{
				$count_number = $count_number + 1;
			}
		}
		return $count_number;
	}

}